<?

if($check_me_authed == "OK"){

	$today_date			 = date("d-m-Y");
  $periode         = " TO_DATE('".$_SESSION["sysPeriode"]."','mm-YYYY') ";

	$refIdTag        = "31";

	$srcDbHdr        = "C_B_HDR";
  $srcDbDtl        = "C_B_DTL";
  $srcDbSubDtl     = "C_B_SUB_DTL";
  $srcDbHdrLog     = "LOG_C_B_HDR";
  $srcDbSequence   = "MST_SEQUENCE";

	if($_POST["selbox"] != ''){

		$UID     = unserialize($_SESSION["serUID"]);
    $userId  = $UID->getUID();

    $Db = new oracleDb;
    $Db->init($_SESSION["ORCL"],
               $_SESSION["OPASS"],
               $_SESSION["SID"],
               _NO_COMMENT);

    if($Db->connect()){

			if(sizeof($_POST["selbox"]) == 0){

				$Misc->usrDieJs("Error: Nothing to approve");
			}else{

				foreach($_POST["selbox"] as $apprThisJobId){

					/**
      		* Fetch global header
      		* And check the status if it is valid
      		* where STATUS = _NOT_ACTIVE
      		*
      		*/
				  $CLFetchers->fetchLstDataHeader($Db,
                                          $apprThisJobId,
                                          $SourceDb,
                                          $nHeader,
                                          $lstHeader);

          $hdrDate = " TO_DATE('".date("d-m-Y",
                                       strtotime($lstHeader["TR_DATE"][0]))."',
                               'dd-mm-YYYY') ";

				  if($lstHeader["STATUS"][0] == _NOT_ACTIVE){

			      /**
			      * Build up the REFERENCE number
			      *
			      */
			      $thisPeriode    = &$_SESSION["sysPeriode"];

			      $seqEditStatus  = false;

            $thisSequence   = "C_B_REF_SEQ";

          	while($seqEditStatus == false){

            	$CFetchers->fetchCurSequence($Db,
                          	               $thisSequence,
                          	               $thisPeriode,
                          	               $srcDbSequence,
                        	                 $srcJidIndex);

            	$CHandlers->initCurSequence($Db,
                          	              $thisSequence,
                          	              $srcJidIndex,
                          	              $thisPeriode,
                          	              $srcDbSequence,
                          	              $seqEditStatus);
            }

            /**
            * Extract the system period
            *
            */
            $tmpDate = explode("-", $thisPeriode);
            $wrkDate = $tmpDate[0]. /* This period month */
                       "-".
                       $tmpDate[1]; /* This period year */

            $refId  = addDigit((string) $srcJidIndex).
                      $srcJidIndex.
                      "/" .
                      $refIdTag.
                      "/" .
                      $wrkDate;

            $CHandlers->procApproval($Db,
                                     $apprThisJobId,
                                     $refId,
                                     $userId,
                                     $srcDbHdr);

            /**
            * Extra work is to input A/P and
            * check the serial number of
            * each tx and do input into inventory
            *
            */
            $jobId     = $apprThisJobId;
            $isApprove = true;

            /**
             * process the mutations
             */
            /**
             * fetch the cash bank tx type
             */
  				  $CLFetchers->fetchCbTxType($Db,
                                       $lstHeader["C_B_CODE"][0],
                                       $SourceDb->fetchValue("srcDbMstCb"),
                                       $cbTxType);

            $CLocal->CbTxType($lstHeader["IN_OUT"][0],
                              $cbTxType,
                              $txType);

            /**
             * process per criteria
             */
            switch($lstHeader["IN_OUT"][0]){

              case _CASH_BANK_IN:

                switch($lstHeader["CASH_BANK_TX_NAME"][0]){

                  case _CASH_BANK_INVOICE_NAME:

                    require("controllers/procInvoiceIn.php");
                    break;

                  case _CASH_BANK_DP_NAME:

                    require("controllers/procDpIn.php");
                    break;

                  case _CASH_BANK_CCL_DP_NAME:

                    require("controllers/procCclDpIn.php");
                    break;

                  default:
                    $Misc->usrDieJs("Error: Tx Type identifier is corrupted");
                }
                break;

              case _CASH_BANK_OUT:

                switch($lstHeader["CASH_BANK_TX_NAME"][0]){

                  case _CASH_BANK_INVOICE_NAME:

                    require("controllers/procInvoiceOut.php");
                    break;

                  case _CASH_BANK_DP_NAME:

                    require("controllers/procDpOut.php");
                    break;

                  case _CASH_BANK_CCL_DP_NAME:

                    require("controllers/procCclDpOut.php");
                    break;

                  default:
                    $Misc->usrDieJs("Error: Tx Type identifier is corrupted");
                }
                break;

              default:
                $Misc->usrDieJs("Error: In/Out identifier is corrupted");
            }

            /**
            * Insert Log
            *
            */
            $CHandlers->insToDbLog($Db,
                                   $today_date,
                                   $apprThisJobId,
                                   $userId,
                                   $_SERVER["REMOTE_ADDR"],
                                   _APPROVE_MODE,
                                   $srcDbHdrLog);

				  }else{

				    $Misc->usrDieJs("Error: Tx is ACTIVE/APPROVED/CANCELED");
				  }

				}

				//$Db->commit();

				if($Db->getStatus() == true){

      	  $Misc->usrMsg("Approval Complete");
      	}else{

      	  $Misc->usrMsg("Approval Process Interupted");
      	}

      	$Db->dc();

			}
		}else{

			$Misc->usrDieJs(_DB_CONN_ERROR);
		}
	}else{

		$Misc->usrDieJs("Error: Unknown selection or bad ID retrieved");
		//jumpTo(2,"../".$_SESSION["source_caller"]);
	}
}else{

	$Misc->usrDieJs("NOT AUTHED");
}
?>